package cn.minezone.preventlogin.utils;

import org.bukkit.Bukkit;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.Plugin;
import org.bukkit.inventory.ItemStack;

import java.io.*;
import java.util.List;

public class ConfigurationsUtil {

    private File file;
    private Plugin plugin;
    private FileConfiguration fileConfiguration;

    public ConfigurationsUtil(String pluginName, String fileName) {
        this.plugin = Bukkit.getPluginManager().getPlugin(pluginName);
        if (!plugin.getDataFolder().exists()) {
            plugin.getDataFolder().mkdir();
        }
        saveDefaultConfig(fileName);
        this.file = new File(plugin.getDataFolder(), fileName);
        this.fileConfiguration = YamlConfiguration.loadConfiguration(this.file);
    }

    public ConfigurationsUtil(Plugin plugin, String fileName) {
        this.plugin = plugin;
        if (!plugin.getDataFolder().exists()) {
            plugin.getDataFolder().mkdir();
        }
        saveDefaultConfig(fileName);
        this.file = new File(plugin.getDataFolder(), fileName);
        this.fileConfiguration = YamlConfiguration.loadConfiguration(this.file);
    }

    private void saveDefaultConfig(String fileName) {
        if (!new File(this.plugin.getDataFolder(), fileName).exists()) {
            //Cannot find file
            InputStream inputStream = this.plugin.getResource(fileName);
            if (inputStream == null) {
                this.plugin.getLogger().warning("Cannot find" + fileName + "in resources");
                return;
            }
            File outFile = new File(this.plugin.getDataFolder(), fileName);
            int lastIndex = fileName.lastIndexOf('/');
            File outDir = new File(this.plugin.getDataFolder(), fileName.substring(0, lastIndex >= 0 ? lastIndex : 0));
            if (!outDir.exists()) {
                outDir.mkdir();
            }
            try {
                OutputStream out = new FileOutputStream(outFile);
                byte[] buf = new byte[1024];
                int len;
                while ((len = inputStream.read(buf)) > 0) {
                    out.write(buf, 0, len);
                }
                out.close();
                inputStream.close();
            } catch (IOException localIOException) {

            }

        }
    }

    public String getString(String path) {
        return this.fileConfiguration.getString(path);
    }

    public String getString(String path, String def) {
        return this.fileConfiguration.getString(path, def);
    }

    public int getInt(String path) {
        return this.fileConfiguration.getInt(path);
    }

    public int getInt(String path, int def) {
        return this.fileConfiguration.getInt(path, def);
    }

    public long getLong(String path) {
        return this.fileConfiguration.getLong(path);
    }

    public long getLong(String path, int def) {
        return this.fileConfiguration.getLong(path, def);
    }

    public ItemStack getItemStack(String path) {
        return this.fileConfiguration.getItemStack(path);
    }

    public ItemStack getItemStack(String path, ItemStack def) {
        return this.fileConfiguration.getItemStack(path, def);
    }

    public boolean getBoolean(String path) {
        return this.fileConfiguration.getBoolean(path);
    }

    public boolean getBoolean(String path, boolean def) {
        return this.fileConfiguration.getBoolean(path, def);
    }

    public List<String> getStringList(String path) {
        return this.fileConfiguration.getStringList(path);
    }

    public List<Integer> getIntegerList(String path) {
        return this.fileConfiguration.getIntegerList(path);
    }

    public List<Double> getDoubleList(String path) {
        return this.fileConfiguration.getDoubleList(path);
    }

    public List<Float> getFloatList(String path) {
        return this.fileConfiguration.getFloatList(path);
    }

    public Object get(String path) {
        return this.fileConfiguration.get(path);
    }

    public void set(String path, Object value) {
        this.fileConfiguration.set(path, value);
    }

    public FileConfiguration getConfig() {
        return this.fileConfiguration;
    }

    public void reloadConfig() {
        this.fileConfiguration = YamlConfiguration.loadConfiguration(this.file);
    }

    public void saveConfig() {
        try {
            this.fileConfiguration.save(this.file);
        }catch (IOException e) {
            this.plugin.getLogger().warning("Cannot save config");
        }

    }

}
